<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :page="page"
               class="collection-list"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="collectUrl" slot-scope="scope" >
        <a style="color:blue" :href="scope.row.collectUrl" target="_blank">{{scope.row.collectUrl}}</a>
      </template>
      <template slot="autoCollectForm" slot-scope="scope">
        <avue-switch v-model="scope.row.autoCollect" :dic="AUTO"></avue-switch>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, add, update, remove } from "@/api/policy/collection";
import { regionTree } from "@/api/organ/region";
import { dictTreeByCode } from "@/api/system/dict";
import { urlRule } from '@/util/validate'

const DIC = {
  LEVEL: [{
    label: '本级',
    value: '0'
  }, {
    label: '本级及下级',
    value: '1'
  }]
}
export default {
  name: "collection",
  inheritAttrs: false,
  data() {
    return {
      AUTO: [{
        label: '否',
        value: '0'
      }, {
        label: '是',
        value: '1'
      }],
      form: {},
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        size: 'small',
        columnBtn: false,
        refreshBtn: false,
        searchMenuSpan: 8,
        menuWidth: 140,
        labelWidth: 120,
        column: [
          {
            label: "所属区划",
            prop: "regionCode",
            type: "tree",
            dicData: [],
            props: {
              value: 'shortCode',
              label: "title"
            },
            search: true,
            width: 120,
            rules: [
              { required: true, message: "请选择所属区划", trigger: "blur" }
            ]
          },
          {
            label: "",
            prop: "regionLevel",
            type: 'radio',
            dicData: DIC.LEVEL,
            search: true,
            searchValue: '0',
            searchSpan: 4,
            searchLabelWidth: '0',
            hide: true,
            display: false
          },
          {
            label: "所属部门",
            prop: "orgName",
            width: 120,
          },
          {
            label: "采集分类",
            prop: "collectType",
            type: "tree",
            dicData: [],
            props: {
              value: 'id',
              label: "title"
            },
            search: true,
            width: 100,
            align: "center",
            rules: [
              { required: true, message: "请选择采集分类", trigger: "blur" }
            ]
          },
          {
            label: "网站名称",
            prop: "websiteName",
            rules: [
              { required: true, message: "请输入网站名称", trigger: "blur" }
            ]
          },
          {
            label: "栏目名称",
            prop: "channelName"
          },
          {
            label: "采集源URL",
            prop: "collectUrl",
            slot: true,
            minWidth: 200,
            rules: [
              { required: true, message: "请输入采集源URL", trigger: "blur" },
              { validator: urlRule, trigger: 'blur' },
            ]
          },
          {
            label: "采集文件名称",
            prop: "collectCsvFile",
            hide: true,
            rules: [
              { required: true, message: "请输入采集文件名称", trigger: "blur" }
            ]
          },
          {
            label: "是否自动采集",
            prop: "autoCollect",
            formslot: true,
            value: "0",
            hide: true,
          },
          {
            label: "采集说明",
            prop: "remark",
            type: "textarea",
            span: 24,
            minRows: 3,
            hide: true
          }
        ]
      },
      data: [],
    }
  },
  created() {
    // 获取字典、区划、部门
    regionTree().then(res => {
      const data = res.data;
      this.setRegionData("regionCode", data);
    });
    dictTreeByCode('policy_collect_type').then(res => {
      const data = res.data;
      this.setDicData("collectType", data);
    });
  },
  methods: {
    onLoad(page, params = {}) {
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data;
        this.page.total = data.total;
        this.data = data.records;
      });
    },
    beforeOpen(done, type) {
      done();
    },
    rowSave(row, loading, done) {
      this.form.regionName = this.form.$regionCode;
      this.form.collectTypeName = this.form.$collectType;
      add(this.form).then(ret => {
        if (ret.success) {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        } else {
          done();
          this.$message({
            type: "error",
            message: ret.msg
          });
        }
      }, error => {
        done();
        console.log(error);
      })
    },
    rowUpdate(row, index, loading, done) {
      this.form.regionName = this.form.$regionCode;
      this.form.collectTypeName = this.form.$collectType;
      update(this.form).then(ret => {
        if (ret.success) {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        } else {
          done();
          this.$message({
            type: "error",
            message: ret.msg
          });
        }
      }, error => {
        done();
        console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定要删除该采集清单吗?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row);
        })
        .then(ret => {
          if (ret.success) {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          } else {
            this.$message({
              type: "error",
              message: ret.msg
            });
          }
        }).catch(() => {
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    // 赋值区划值
    setRegionData(key, res) {
      const index = this.$refs.crud.findColumnIndex(key);
      this.option.column[index].dicData = res;
    },
    // 赋值字典值
    setDicData(key, res) {
      const index = this.$refs.crud.findColumnIndex(key);
      this.option.column[index].dicData = res;
    },
  }
};
</script>

<style scoped lang="scss">
  @import "~_s/placeholders";
  @import "~_s/table-list";

  ::v-deep .collection-list {
    @extend %fullsize;
    @extend %table-list;
  }
</style>